//package com.b2c.common.third.youzan;
//
//import com.alibaba.fastjson.JSONObject;
//import com.b2c.common.utils.DateUtil;
//import com.b2c.common.utils.JsonUtil;
//import com.youzan.cloud.open.sdk.common.exception.SDKException;
//import com.youzan.cloud.open.sdk.core.client.core.DefaultYZClient;
//import com.youzan.cloud.open.sdk.core.oauth.model.OAuthToken;
//import com.youzan.cloud.open.sdk.core.oauth.token.TokenParameter;
//import com.youzan.cloud.open.sdk.core.client.auth.Token;
//import com.youzan.cloud.open.sdk.gen.v1_0_0.api.YouzanUserOpenidGet;
//import com.youzan.cloud.open.sdk.gen.v1_0_0.model.YouzanUserOpenidGetParams;
//import com.youzan.cloud.open.sdk.gen.v1_0_0.model.YouzanUserOpenidGetResult;
//import com.youzan.cloud.open.sdk.gen.v3_0_0.api.*;
//import com.youzan.cloud.open.sdk.gen.v3_0_0.model.*;
//import com.youzan.cloud.open.sdk.gen.v4_0_0.api.YouzanTradesSoldGet;
//import com.youzan.cloud.open.sdk.gen.v4_0_0.model.YouzanTradesSoldGetParams;
//import com.youzan.cloud.open.sdk.gen.v4_0_0.model.YouzanTradesSoldGetResult;
//import org.springframework.util.StringUtils;
//
//import java.io.File;
//import java.util.Date;
//
//
///**
// * @Description:有赞获取数据类 pbd add 2019/6/6 9:06
// */
//public class YouzanClient {
//    private static DefaultYZClient yzClient = new DefaultYZClient();
//    private static String clinetId = "697d095174bf6db797";
//    private static String clientSecret = "a1bcda1c92fe98e1767545c7d34b6ec3";
//    private static String grantId = "43061102";
//
//    /**
//     * 获取有赞token
//     */
//    public static Token getToken() {
//        //2.调用获取Token Api
//        Token token = null;
//        for (int i = 0, n = 100; i < n; i++) {
//            try {
//                TokenParameter tokenParameter = TokenParameter.self().clientId(clinetId).clientSecret(clientSecret).grantId(grantId).build();
//                OAuthToken codeToken = yzClient.getOAuthToken(tokenParameter);
//                if (StringUtils.isEmpty(codeToken.getAccessToken()) == false) {
//                    token = new Token(codeToken.getAccessToken());
//                    break;
//                }
//            } catch (Exception e) {
//                continue;
//            }
//        }
//        return token;
//    }
//
//    /**
//     * 查询有赞上架中的商品列表
//     *
//     * @return
//     * @throws Exception
//     */
//    public static YouzanItemsOnsaleGetResult queryOnsaleGoods(Integer pageNo, Integer pageSize) {
//        try {
//            YouzanItemsOnsaleGet youzanItemsOnsaleGet = new YouzanItemsOnsaleGet();
//            YouzanItemsOnsaleGetParams youzanItemsOnsaleGetParams = new YouzanItemsOnsaleGetParams();
//            youzanItemsOnsaleGetParams.setPageNo(pageNo);
//            youzanItemsOnsaleGetParams.setPageSize(pageSize);
//            youzanItemsOnsaleGet.setAPIParams(youzanItemsOnsaleGetParams);
//            return yzClient.invoke(youzanItemsOnsaleGet, getToken(), YouzanItemsOnsaleGetResult.class);
//        } catch (Exception e) {
//            System.out.println("查询有赞上架中的商品列表异常:" + e);
//        }
//        return null;
//    }
//
//    /**
//     * 根据userId（账号id）查询有赞openId
//     *
//     * @return
//     * @throws Exception
//     */
//    public static YouzanUserOpenidGetResult userOpenidGetResult(Long userId) {
//        try {
//            YouzanUserOpenidGetParams youzanUserOpenidGetParams = new YouzanUserOpenidGetParams();
//            youzanUserOpenidGetParams.setUserId(userId);
//            YouzanUserOpenidGet youzanUserOpenidGet = new YouzanUserOpenidGet();
//            youzanUserOpenidGet.setAPIParams(youzanUserOpenidGetParams);
//            return yzClient.invoke(youzanUserOpenidGet, getToken(), YouzanUserOpenidGetResult.class);
//        } catch (Exception e) {
//            System.out.println("根据userId（账号id）查询有赞openId异常:" + e);
//        }
//        return null;
//    }
//
//    /**
//     * 查询有赞仓库中的商品
//     */
//    public static YouzanItemsInventoryGetResult queryInventorygooods(Integer pageNo, Integer pageSize) {
//        try {
//            YouzanItemsInventoryGetParams youzanItemsInventoryGetParams = new YouzanItemsInventoryGetParams();
//            youzanItemsInventoryGetParams.setPageNo(pageNo);
//            youzanItemsInventoryGetParams.setPageSize(pageSize);
//            YouzanItemsInventoryGet youzanItemsInventoryGet = new YouzanItemsInventoryGet();
//            youzanItemsInventoryGet.setAPIParams(youzanItemsInventoryGetParams);
//            return yzClient.invoke(youzanItemsInventoryGet, getToken(), YouzanItemsInventoryGetResult.class);
//        } catch (Exception e) {
//
//        }
//        return null;
//
//    }
//
//    /**
//     * 根据商品id查询有赞商品详情
//     *
//     * @return
//     */
//    public static YouzanItemGetResult queryGoodDetail(Long yzItemId) {
//        try {
//            TokenParameter tokenParameter = TokenParameter.self().clientId(clinetId).clientSecret(clientSecret).grantId(grantId).build();
//            OAuthToken codeToken = yzClient.getOAuthToken(tokenParameter);
//            YouzanItemGet youzanItemGet = new YouzanItemGet();
//            YouzanItemGetParams youzanItemGetParams = new YouzanItemGetParams();
//            youzanItemGetParams.setItemId(yzItemId);
//            youzanItemGet.setAPIParams(youzanItemGetParams);
//            YouzanItemGetResult result = yzClient.invoke(youzanItemGet, new Token(codeToken.getAccessToken()), YouzanItemGetResult.class);
//            if (result.getCode() == 200 && null != result) return result;
//        } catch (Exception e) {
//            System.out.println("根据商品id查询有赞商品详情异常:" + e);
//        }
//        return null;
//    }
//
//    /**
//     * 获取有赞订单
//     *
//     * @return
//     */
//    public static YouzanTradesSoldGetResult queryOrders(Integer pageNo, Integer pageSize) {
//        try {
//            YouzanTradesSoldGet youzanTradesSoldGet = new YouzanTradesSoldGet();
//            YouzanTradesSoldGetParams youzanTradesSoldGetParams = new YouzanTradesSoldGetParams();
//            youzanTradesSoldGetParams.setPageNo(pageNo);//开始页码
//            youzanTradesSoldGetParams.setPageSize(pageSize);//结束页码
//            youzanTradesSoldGetParams.setStartCreated(DateUtil.getIntervalDate(-3));//订单创建时间
//            youzanTradesSoldGetParams.setEndCreated(new Date());//订单结束时间
//            youzanTradesSoldGet.setAPIParams(youzanTradesSoldGetParams);
//            return yzClient.invoke(youzanTradesSoldGet, getToken(), YouzanTradesSoldGetResult.class);
//        } catch (Exception e) {
//            System.out.println("查询有赞订单记录异常:" + e);
//        }
//        return null;
//    }
//
//    /**
//     * 获取店铺信息
//     *
//     * @return
//     */
//    public static YouzanShopGetResult shopGetResult() {
//        try {
//            YouzanShopGet youzanShopGet = new YouzanShopGet();
//            //创建参数对象,并设置参数
//            YouzanShopGetParams youzanShopGetParams = new YouzanShopGetParams();
//            youzanShopGet.setAPIParams(youzanShopGetParams);
//            return yzClient.invoke(youzanShopGet, getToken(), YouzanShopGetResult.class);
//        } catch (Exception e) {
//            System.out.println("获取店铺信息异常:" + e);
//        }
//        return null;
//    }
//
//    /**
//     * 获取物流信息
//     *
//     * @return
//     */
//    public static YouzanLogisticsExpressbyordernoGetResult expressbyordernoGet(String tid) {
//        try {
//            YouzanLogisticsExpressbyordernoGet youzanLogisticsExpressbyordernoGet = new YouzanLogisticsExpressbyordernoGet();
//            YouzanLogisticsExpressbyordernoGetParams youzanLogisticsExpressbyordernoGetParams = new YouzanLogisticsExpressbyordernoGetParams();
//            youzanLogisticsExpressbyordernoGetParams.setTid(tid);
//            youzanLogisticsExpressbyordernoGet.setAPIParams(youzanLogisticsExpressbyordernoGetParams);
//            return yzClient.invoke(youzanLogisticsExpressbyordernoGet, getToken(), YouzanLogisticsExpressbyordernoGetResult.class);
//        } catch (Exception e) {
//            System.out.println("获取店铺信息异常:" + e);
//        }
//        return null;
//    }
//
//    /**
//     * 更新物流信息
//     *
//     * @param yz_tid       有赞订单号
//     * @param yz_out_stype 快递公司id
//     * @param yz_out_sid   快递单号
//     * @return
//     */
//    public static YouzanLogisticsOnlineConfirmResult updOrderLogistics(String yz_tid, String yz_out_stype, String yz_out_sid) {
//        try {
//            YouzanLogisticsOnlineConfirm youzanLogisticsOnlineConfirm = new YouzanLogisticsOnlineConfirm();
//            YouzanLogisticsOnlineConfirmParams youzanLogisticsOnlineConfirmParams = new YouzanLogisticsOnlineConfirmParams();
//            //快递公司编号
//            youzanLogisticsOnlineConfirmParams.setOutStype(yz_out_stype);
//            //订单编号
//            youzanLogisticsOnlineConfirmParams.setTid(yz_tid);
//            //是否是无需物流0表示物流发货1表示无需物流
//            youzanLogisticsOnlineConfirmParams.setIsNoExpress(0);
//            //快递单号（具体一个物流公司的真实快递单号）
//            youzanLogisticsOnlineConfirmParams.setOutSid(yz_out_sid);
//            youzanLogisticsOnlineConfirm.setAPIParams(youzanLogisticsOnlineConfirmParams);
//            return yzClient.invoke(youzanLogisticsOnlineConfirm, getToken(), YouzanLogisticsOnlineConfirmResult.class);
//        } catch (Exception e) {
//            System.out.println("查询有赞订单记录异常:" + e);
//        }
//        return null;
//    }
//
//    /**
//     * 更新商品规格
//     *
//     * @param itemId    商品Id
//     * @param skuStocks JSON 数组规格参数[{"price":10000,"quantity":100,"item_no":"XIGUA-1","skus":[{"k":"颜色","kid":1,"v":"红色","vid":1111},]},...];
//     * @return
//     */
//    public static YouzanItemUpdateResult updGood(Long itemId, String skuStocks, String itemNo) {
//        try {
//            YouzanItemUpdate youzanItemUpdate = new YouzanItemUpdate();
//            //创建参数对象,并设置参数
//            YouzanItemUpdateParams youzanItemUpdateParams = new YouzanItemUpdateParams();
//            youzanItemUpdateParams.setItemNo(itemNo);
//            youzanItemUpdateParams.setItemId(itemId);
//            youzanItemUpdateParams.setSkuStocks(skuStocks);
//            youzanItemUpdate.setAPIParams(youzanItemUpdateParams);
//            return yzClient.invoke(youzanItemUpdate, getToken(), YouzanItemUpdateResult.class);
//        } catch (Exception e) {
//            System.out.println("更新商品规格异常:" + e);
//        }
//        return null;
//    }
//
//    /**
//     * 查询有赞快递信息
//     *
//     * @return
//     */
//    public static YouzanLogisticsExpressGetResult queryExpress() {
//        try {
//            YouzanLogisticsExpressGet youzanLogisticsExpressGet = new YouzanLogisticsExpressGet();
//            YouzanLogisticsExpressGetParams youzanLogisticsExpressGetParams = new YouzanLogisticsExpressGetParams();
//            youzanLogisticsExpressGet.setAPIParams(youzanLogisticsExpressGetParams);
//            return yzClient.invoke(youzanLogisticsExpressGet, getToken(), YouzanLogisticsExpressGetResult.class);
//        } catch (Exception e) {
//            System.out.println("查询有赞订单记录异常:" + e);
//        }
//        return null;
//    }
//
//    /**
//     * 上传有赞图片
//     *
//     * @param img
//     * @return
//     */
//    public static YouzanMaterialsStoragePlatformImgUploadResult imgUploadResult(File img) {
//        try {
//            YouzanMaterialsStoragePlatformImgUpload youzanMaterialsStoragePlatformImgUpload = new YouzanMaterialsStoragePlatformImgUpload();
//            //创建参数对象,并设置参数
//            YouzanMaterialsStoragePlatformImgUploadParams youzanMaterialsStoragePlatformImgUploadParams = new YouzanMaterialsStoragePlatformImgUploadParams();
//            youzanMaterialsStoragePlatformImgUploadParams.setImage(img);
//            youzanMaterialsStoragePlatformImgUpload.setAPIParams(youzanMaterialsStoragePlatformImgUploadParams);
//            return yzClient.invoke(youzanMaterialsStoragePlatformImgUpload, getToken(), YouzanMaterialsStoragePlatformImgUploadResult.class);
//        } catch (SDKException e) {
//            System.out.println("图片新增异常:" + e);
//        }
//        return null;
//    }
//
//
//    /**
//     * 发布有赞商品,新增商品
//     * title 商品标题, desc 商品描述,item_no 商品编号,price 价格 单位分 ,image_ids 图片Id列表,sku_stocks JSON集合 ,is_display 是否上架商品
//     *
//     * @return
//     */
//    public static YouzanItemCreateResult itemCreate(String title, String desc, String item_no, Long price, String image_ids, String sku_stocks, Integer is_display) {
//        try {
//            YouzanItemCreate youzanItemCreate = new YouzanItemCreate();
//            //创建参数对象,并设置参数
//            YouzanItemCreateParams youzanItemCreateParams = new YouzanItemCreateParams();
//            youzanItemCreateParams.setTitle(title);
//            youzanItemCreateParams.setDesc(desc);
//            youzanItemCreateParams.setItemNo(item_no);
//            youzanItemCreateParams.setPrice(price);
//            youzanItemCreateParams.setImageIds(image_ids);
//            youzanItemCreateParams.setSkuStocks(sku_stocks);
//            youzanItemCreateParams.setIsDisplay(is_display);
//            youzanItemCreate.setAPIParams(youzanItemCreateParams);
//            return yzClient.invoke(youzanItemCreate, getToken(), YouzanItemCreateResult.class);
//        } catch (Exception e) {
//            System.out.println("发布有赞商品异常:" + e);
//        }
//        return null;
//    }
//
//    /**
//     * 根据商品id获取商品信息
//     *
//     * @return
//     */
//    public static YouzanItemGetResult getResult(Long id) {
//        try {
//            YouzanItemGet youzanItemGet = new YouzanItemGet();
//            //创建参数对象,并设置参数
//            YouzanItemGetParams youzanItemGetParams = new YouzanItemGetParams();
//            youzanItemGetParams.setItemId(id);
//            youzanItemGet.setAPIParams(youzanItemGetParams);
//
//            return yzClient.invoke(youzanItemGet, getToken(), YouzanItemGetResult.class);
//        } catch (Exception e) {
//            System.out.println("获取有赞商品异常:" + e);
//        }
//        return null;
//    }
//
//    /**
//     * 根据编码确定有赞是否有该编码
//     *
//     * @return
//     */
//    public static YouzanItemsCustomGetResult getResult(String item) {
//        try {
//            YouzanItemsCustomGetParams youzanItemsCustomGetParams = new YouzanItemsCustomGetParams();
//            YouzanItemsCustomGet youzanItemsCustomGet = new YouzanItemsCustomGet();
//            youzanItemsCustomGetParams.setItemNo(item);
//            youzanItemsCustomGet.setAPIParams(youzanItemsCustomGetParams);
//
//            return yzClient.invoke(youzanItemsCustomGet, getToken(), YouzanItemsCustomGetResult.class);
//        } catch (Exception e) {
//            System.out.println("获取有赞编码异常:" + e);
//        }
//        return null;
//    }
//
//
//    public static void main(String[] args) {
//
///*        YouzanItemGetResult result = YouzanClient.getResult((long) 48144739);
//        System.out.println(JSONObject.toJSONString(result.getSuccess()));*/
//        System.out.println(getToken());
//
//    }
//
//
//}
